package com.shitou.springai1.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shitou.springai1.admin.entity.dto.PostDto;
import com.shitou.springai1.admin.mapper.PostMapper;
import com.shitou.springai1.admin.mapper.UserMapper;
import com.shitou.springai1.admin.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.HashMap;
import java.util.Map;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, PostDto> implements PostService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<PostDto> getPostsByPage(Integer pageNum, Integer pageSize, String keyword, 
                                       String startDate, String endDate, Integer status, String likeOrder) {
        // 创建分页对象
        Page<PostDto> page = new Page<>(pageNum, pageSize);

        // 创建查询条件构造器
        QueryWrapper<PostDto> queryWrapper = new QueryWrapper<>();

        // 关键词搜索
        if (keyword != null && !keyword.isEmpty()) {
            queryWrapper.like("content", keyword);
        }

        // 时间范围筛选
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
            if (startDate != null && !startDate.isEmpty()) {
                LocalDate start = LocalDate.parse(startDate, formatter);
                queryWrapper.ge("create_time", start);
            }
            if (endDate != null && !endDate.isEmpty()) {
                LocalDate end = LocalDate.parse(endDate, formatter);
                queryWrapper.le("create_time", end);
            }
        } catch (DateTimeParseException e) {
            // 如果日期格式不正确，忽略日期筛选条件
            System.err.println("日期格式不正确，忽略日期筛选条件: " + e.getMessage());
        }

        // 状态筛选
        if (status != null) {
            queryWrapper.eq("status", status);
        }

        // 点赞数排序
        if ("asc".equalsIgnoreCase(likeOrder)) {
            queryWrapper.orderByAsc("like_count");
        } else {
            queryWrapper.orderByDesc("like_count");
        }

        // 最后按创建时间排序
        queryWrapper.orderByDesc("create_time");

        // 使用MyBatis-Plus的page方法进行分页查询
        return page(page, queryWrapper);
    }

    @Override
    public boolean updatePostStatus(Long id, Integer status) {
        // 验证参数
        if (id == null || status == null) {
            return false;
        }

        // 更新言论状态
        PostDto post = new PostDto();
        post.setId(id);
        post.setStatus(status);
        return updateById(post);
    }

    @Override
    public Map<Long, String> getUserNamesByIds(Iterable<Long> userIds) {
        // 这里简化实现，实际应该查询用户表获取用户名
        Map<Long, String> userNameMap = new HashMap<>();
        // 假设这里是从用户表查询用户名的逻辑
        // 由于目前没有完整的用户表信息，返回模拟数据
        userIds.forEach(userId -> {
            userNameMap.put(userId, "用户" + userId);
        });
        return userNameMap;
    }
}
